﻿
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="box box-info">
    <!-- /.box-header -->
    <!-- form start -->
    <form id="form1" class="form-horizontal">
        <div class="box-body">
            <div class="form-group">
                <label for="inputEmail3" class="col-sm-2 control-label">新密码：</label>

                <div class="col-sm-10">
                    <input type="password" class="form-control" id="newpwd" name="newpwd">
                </div>
            </div>
            <div class="form-group">
                <label for="inputPassword3" class="col-sm-2 control-label">确认密码：</label>

                <div class="col-sm-10">
                    <input type="password" class="form-control" id="newpwd2" name="newpwd2">
                </div>
            </div>
        </div>
        <!-- /.box-body -->
        <div class="box-footer text-center">
            <button class="btn btn-primary" type="button" id="btnSave">确 定 </button>
            <button class="btn" type="button" id="cancel">取 消 </button>
        </div>
        <!-- /.box-footer -->
    </form>
    @section scriptsBottom{
        <script type="text/javascript">
            $(function () {
                initFormValidate();
                $("#btnSave").click(function () {
                    save();
                });
                $("#cancel").click(function () {
                    BMCW.closeTopLayer();
                });
            });
            function initFormValidate() {
                $("#form1").bootstrapValidator({
                    submitHandler: function (valiadtor, loginForm, submitButton) {
                        valiadtor.defaultSubmit();
                    },
                    fields: {
                        newpwd: {
                            validators: {
                                notEmpty: {
                                    message: '请输入新密码'
                                }
                            }
                        },
                        newpwd2: {
                            validators: {
                                notEmpty: {
                                    message: '请确认一遍密码'
                                }
                            }
                        }
                    }
                });
            }
            function save() {
                var bootstrapValidator = $("#form1").data('bootstrapValidator');
                bootstrapValidator.validate();
                if (bootstrapValidator.isValid()) {
                    var pwd = $("#newpwd").val();
                    var pwd2 = $("#newpwd2").val();
                    var reg = /^(?![0-9a-z]+$)(?![0-9A-Z]+$)(?![0-9\W]+$)(?![a-z\W]+$)(?![a-zA-Z]+$)(?![A-Z\W]+$)[a-zA-Z0-9\W_]{8,}$/;
                    if (!reg.test(pwd)) {
                        BMCW.alert("密码格式不正确（长度不小于8位，且必须包含英文字符、数字及特殊符号）");
                    } else if (pwd != pwd2) {
                        BMCW.alert("两次输入的密码不一致，请重新输入");
                    } else {
                        var option = {
                            type:"POST",
                            url: '@Url.Action("EditPasswordSave","User")',
                            data: {
                                UserId:@ViewBag.UserId,
                                NewPwd: pwd
                            },
                            success: function (o) {
                                if (o.status == 0) {
                                    BMCW.msg("保存成功", function () {
                                        BMCW.getTopSelectedWin().search();
                                    });
                                } else {
                                    BMCW.alert("保存失败");
                                }
                            }
                        };
                        BMCW.ajax(option);
                    }
                }
            }

        </script>
    }
</div>
